package com.pristine.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pristine.dao.ICountryMasterDao;
import com.pristine.domain.CountryMasterEntity;
import com.pristine.domain.StateMasterEntity;
import com.pristine.service.wsdl.CountryMasterVO;
import com.pristine.service.wsdl.ICountryMasterService;
import com.pristine.service.wsdl.StateMasterVO;
import com.pristine.util.EntityToVOConverter;
import com.pristine.util.VOToEntityConverter;

@Service("countryMasterService")
public class CountryMasterServiceImpl implements ICountryMasterService{

	@Autowired
	ICountryMasterDao countryMasterDao;
	@Override
	public CountryMasterVO getCountryMasterById(Integer id) {
		CountryMasterEntity countryMaster = countryMasterDao.find(id);
		CountryMasterVO masterVO= EntityToVOConverter.convert(countryMaster);
		return masterVO;
	}

	@Override
	@Transactional
	public boolean deleteCountryMaster(Integer id) {
		try{
			CountryMasterEntity countryMaster = countryMasterDao.find(id);
			countryMasterDao.remove(countryMaster);
			return true;
		}catch(Exception e){
			return false;
		}
		
	}

	@Override
	@Transactional
	public Integer updateCountryMaster(CountryMasterVO masterVO) {
		CountryMasterEntity countryMaster= VOToEntityConverter.convert(masterVO);
		countryMasterDao.update(countryMaster);
		return countryMaster.getId();
	}

	@Override
	@Transactional
	public Integer createCountryMaster(CountryMasterVO masterVO) {
		CountryMasterEntity countryMaster= VOToEntityConverter.convert(masterVO);
		countryMasterDao.add(countryMaster);
		return countryMaster.getId();
	}

	@Override
	@Transactional
	public List<StateMasterVO> getStatesByCountryId(Integer countryId) {
		List<StateMasterEntity> listStates = countryMasterDao.getStatesByCountryId(countryId);
		List<StateMasterVO> voList=  new ArrayList<StateMasterVO>();
		if(CollectionUtils.isNotEmpty(listStates)){
			for(StateMasterEntity entity:listStates){
				voList.add(EntityToVOConverter.convert(entity));
			}
		}
		return voList;
	}

	@Override
	@Transactional
	public List<CountryMasterVO> getAllCountry() {
		List<CountryMasterEntity> list = countryMasterDao.list();
		List<CountryMasterVO> voList=  new ArrayList<CountryMasterVO>();
		if(CollectionUtils.isNotEmpty(list)){
			for(CountryMasterEntity entity:list){
				voList.add(EntityToVOConverter.convert(entity));
			}
		}
		return voList;
	}
	
	
}
